home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / zmdm / tyme.c < prev    next >
C/C++ Source or Header  |  1993-06-26  |  5KB  |  217 lines

  1. /*
  2.  *     Time conversions Module
  3.  *
  4.  *        Jwahar Bammi
  5.  *            usenet: cwruecmp!bammi@decvax.UUCP
  6.  *            csnet:  bammi@cwru.edu
  7.  *            arpa:   bammi@cwru.edu
  8.  *            CompuServe: 71515,155
  9.  */
  10.  
  11. #include <stdio.h>
  12.  
  13. /******************************* CONFIGURABLE ********************************\
  14. *    Number of seconds from GMT (signed)                      *
  15. \*****************************************************************************/
  16.  
  17. #define GMTDIFF    (-5L*3600L)    /* EST -ve means behind GMT */
  18.  
  19. /*****************************************************************************/
  20.  
  21.  
  22. /* #define DEBUG  */
  23. /* #define SDEBUG */
  24.  
  25. #define J702J80 318211200L    /* # of seconds from Jan 70 to Jan 80 */
  26.  
  27. /*
  28.  * days in a given year
  29.  */
  30. #define days_in_year(Y) (leap(Y) ? 366 : 365)
  31.  
  32. /* # of days / month in a normal year */
  33. static unsigned int md[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  34. static int leap (y)
  35. int y;
  36. {
  37.     y += 1900;
  38.     if ((y % 400) == 0)
  39.         return (1);
  40.     if ((y % 100) == 0)
  41.         return (0);
  42.     return ((y % 4) == 0);
  43. }
  44.  
  45. /* Return the number of days between Jan 1, Given Year and the given
  46.  * broken-down time.
  47.  */
  48.  
  49. static unsigned long ndays (since, year, month, day)
  50. unsigned int year, month, day;
  51. {
  52.     register unsigned long n = day;
  53.     register unsigned int m, y;
  54.     
  55.     for (y = since; y < year; y++)
  56.     {
  57.         n += 365;
  58.         if (leap (y)) n++;
  59.     }
  60.     for (m = 0; m < (month-1); m++)
  61.         n += md[m] + ( ((m == 1) && leap(y))? 1 : 0);
  62.  
  63.     return (n);
  64. }
  65.  
  66. /* Convert a broken-down time into seconds
  67.  *
  68.  */
  69.  
  70. unsigned long tm_to_time (base_year, year, month, day, hours, mins, secs)
  71. unsigned int base_year, year, month, day, hours, mins, secs;
  72. {
  73.     register unsigned long t;
  74.     extern unsigned long ndays();
  75.     
  76.     t = (ndays(base_year, year, month, day) - 1L) * (unsigned long)86400L
  77.         + hours * (unsigned long)3600L + mins * (unsigned long)60L + secs;
  78.  
  79.     return t;
  80. }
  81.  
  82. /*
  83.  * Convert ST time to Unix time
  84.  *
  85.  */
  86. unsigned long st2unix(time, date)
  87. unsigned int time, date;
  88. {
  89.     extern unsigned long tm_to_time();
  90.     
  91.     unsigned int yr = ((date >> 9) & 0x007f) + 80;  /* dissect the date */
  92.     unsigned int mo = (date >> 5) & 0x000f;
  93.     unsigned int dy = date & 0x1f;
  94.     
  95.     unsigned int hr = (time >> 11) & 0x001f;        /* dissect the time */
  96.     unsigned int mm = (time >> 5)  & 0x003f;
  97.     unsigned int ss = (time & 0x001f) * 2;
  98.  
  99. #ifdef SDEBUG
  100.     printf("%d/%d/%d  %d:%d:%d\n", mo, dy, yr,hr, mm, ss);
  101. #endif
  102.     return (tm_to_time(70, yr, mo, dy, hr, mm, ss) -
  103.         (unsigned long)GMTDIFF);
  104. }
  105.  
  106. /*
  107.  * Convert Unix Time to ST Time
  108.  *
  109.  */
  110. void unix2st(Unix, time, date)
  111. unsigned long Unix;
  112. unsigned int *date, *time;
  113. {
  114.     long stbase;
  115.     unsigned int hours, yrs, day, months, mins, seconds, t;
  116.     long days, secs;
  117.     extern unsigned long tm_to_time();
  118.  
  119. #ifdef DEBUG
  120. printf("\n\nUnix Time %ld\n", Unix);
  121. #endif
  122.  
  123.     
  124. /*    if((Unix - J702J80) <= 0) */    /* base 1980 */
  125.     if((Unix - tm_to_time(70, 80, 0, 0, 0, 0, 0)) <= 0)  /* base 1980 */
  126.     {
  127.         /* thats before St's time */
  128.         *time = 0;
  129.         *date = (1 << 5) | 1;    /* Jan 1, 1980 00:00:00 GMT */
  130. #ifdef DEBUG
  131. printf("Before my time\n");
  132. #endif
  133.         return;
  134.     }
  135.  
  136.     stbase = Unix;    /* do from base year 1970 */
  137.  
  138.     days = stbase / 86400L;    /* 3600*24 */
  139.     secs = stbase % 86400L + GMTDIFF;
  140.     if(secs < 0)    /* previous day here */
  141.     {
  142.         days -= 1;
  143.         secs += 86400L;
  144.     }
  145.  
  146.     /* extract hrs : mins : seconds */
  147.     hours = secs / 3600;
  148.     secs = secs - (hours * 3600);
  149.     mins = secs / 60;
  150.     seconds = secs - (mins * 60);
  151.     seconds &= ~1L;            /* ST has 2 sec resolution */
  152.  
  153.     /* get the year and day of the year */
  154.     for(t = 70; days >= days_in_year(t); t++)
  155.         days -= days_in_year(t);
  156.     yrs = t;
  157.     day = days;
  158.  
  159.     /* get the month */
  160.     if(days_in_year(yrs) == 366)
  161.         md[1] = 29;
  162.  
  163.     for(t = 0; day >= md[t]; t++)
  164.         day -= md[t];
  165.  
  166.     md[1] = 28;        
  167.     day = day + 1;
  168.     months = t + 1;
  169. #ifdef DEBUG
  170. printf("%d/%d/%d   %d:%d:%d\n", months, day, yrs, hours, mins, seconds);
  171. #endif
  172.  
  173.     yrs -= 80;
  174.     *date = (((yrs & 0x007f) << 9) | ((months & 0x000f) << 5)
  175.           | (day & 0x001f));
  176.     
  177.     *time = (((hours & 0x001f) << 11) | ((mins & 0x003f) << 5)
  178.           | (seconds & 0x001e));
  179. }
  180.  
  181. #ifdef TEST
  182. #include <stdio.h>
  183. #include <osbind.h>
  184.  
  185. main()
  186. {
  187.     unsigned int time, date;
  188.     unsigned long Unix;
  189.     extern unsigned long st2unix();
  190.     
  191.     time = Tgettime();
  192.     date = Tgetdate();
  193.     Unix = st2unix(time, date);
  194.  
  195.     printd(time, date);
  196.     printf("Unix Time %ld\n", Unix);
  197.  
  198.     unix2st(Unix, &time, &date);
  199.     printd(time, date);
  200. }
  201.  
  202. printd(time, date)
  203. unsigned int time, date;
  204. {
  205.     
  206.     unsigned int yr = ((date >> 9) & 0x007f) + 80;  /* dissect the date */
  207.     unsigned int mo = (date >> 5) & 0x000f;
  208.     unsigned int dy = date & 0x1f;
  209.     
  210.     unsigned int hr = (time >> 11) & 0x001f;        /* dissect the time */
  211.     unsigned int mm = (time >> 5)  & 0x003f;
  212.     unsigned int ss = (time & 0x001f) * 2;
  213.  
  214.     printf("%d/%d/%d\t%d:%d:%d\n", mo, dy, yr, hr, mm, ss);
  215. }
  216. #endif /* TEST */
  217.